using System;
using LocalizationUtils;
using LocalizationUtils.Language;
using LocalizationUtils.Utils;
using TMPro;
using UnityEngine;

namespace LocalizationUtils
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(RectTransform))]
    [AddComponentMenu("UI/LocalizationTMPText", 11)]
    [ExecuteAlways]
    public class LocalizationTMPText : TextMeshProUGUI, ILocalization
    {
        public bool _needAwake = true;
        public long _key;


        public override string text
        {
            get { return base.text; }
            set => SetUIText(value);
        }

        public long key { get; set; }

        protected override void Awake()
        {
            base.Awake();
            if (Application.isPlaying && _needAwake)
            {
                if (_key == 0)
                    return;

                text = Lang.Get(_key);
            }

            AdjustArabRight();
        }

        public void AdjustArabRight()
        {
            if (LocalizationManager.CurrSystemLanguage == LocalizationManager.CustomSystemLanguage.Arabic)
            {
                if (this.horizontalAlignment == HorizontalAlignmentOptions.Left)
                {
                    this.horizontalAlignment = HorizontalAlignmentOptions.Right;
                }
            }
        }

        void SetUIText(string content)
        {
            if (LocalizationManager.CurrSystemLanguage == LocalizationManager.CustomSystemLanguage.Arabic)
            {
                isRightToLeftText = true;
                base.text = Utils.LocalizationUtils.GetFixedText(content);
            }
            else
            {
                isRightToLeftText = false;
                base.text = content;
            }
        }
    }
}